﻿using System;
using System.Windows.Media;
using WPControls;
using System.Collections.ObjectModel;
using System.Windows;
using RATSharpW7.Model;

namespace RATSharpW7.Converters
{
    /// <summary>
    /// Converter for calendars month view. 
    /// </summary>
    public class CalendarColorConverter : FrameworkElement, IDateToBrushConverter
    {
        /// <summary>
        /// Bindable occupation collection - there should be placed all
        /// items which should be colored in blue
        /// </summary>
        public ObservableCollection<Occupation> Occupations
        {
            get { return (ObservableCollection<Occupation>)GetValue(OccupationsProperty); }
            set { SetValue(OccupationsProperty, value); }
        }

        public static readonly DependencyProperty OccupationsProperty =
           DependencyProperty.Register("Occupations", typeof(ObservableCollection<Occupation>), typeof(CalendarColorConverter),
           new PropertyMetadata(new ObservableCollection<Occupation>(), new PropertyChangedCallback(OnOccupationsChanged)));

        private static void OnOccupationsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((CalendarColorConverter)d).OnOccupationsChanged(e);
        }

        protected virtual void OnOccupationsChanged(DependencyPropertyChangedEventArgs e)
        {
        }

        /// <summary>
        /// Color all days with reservation to blue, today's day in red
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="isSelected"></param>
        /// <param name="defaultValue"></param>
        /// <param name="brushType"></param>
        /// <returns></returns>
        public Brush Convert(DateTime dateTime, bool isSelected, Brush defaultValue, BrushType brushType)
        {
            if (brushType == BrushType.Background)
            {
                if (dateTime.Date.Equals(DateTime.Now.Date))
                {
                    return new SolidColorBrush(Colors.Red);
                }
                foreach(Occupation occ in Occupations)
                {
                    if (occ.date.Equals(dateTime.Date))
                    {
                        return new SolidColorBrush(Colors.Blue);
                    }
                }

               return defaultValue;
            }
            else
            {
                return defaultValue;
            }
        }
    }
}
